<?php
/**
 * Handle MySQL and rail connection. Provide functions to create new rail structures.
 *
 * PHP versions 5
 *
 * Copyright (C)
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2007-07-28
 * @package rail
 */


/* ------------------------------------ Listener -------------------------------------- */
// listen: logout
if( isset($_GET['logout']) ){
	session_destroy();
	session_start();
}

// listen: if form send and a MySQL host is set
//
if( isset($_POST) && isset($_POST["host"]) && strlen($_POST["host"]) > 0 ){
	// reset session
	session_destroy();
	session_start();
	// try to connect to MySQL and rail
	try{
		$mysqli = new mysqli( $_POST["host"], $_POST["user"], $_POST["passwd"] );
		if( mysqli_connect_errno() )
			throw new Exception( mysqli_connect_error() );

		$_SESSION['mysql_host'] = $_POST["host"];
		$_SESSION['mysql_user'] = $_POST["user"];
		$_SESSION['mysql_passwd'] = $_POST["passwd"];

		// select all MySQL databases
		$data = $mysqli->query( "SHOW DATABASES" );
		while( $data_row = $data->fetch_assoc() )
			$mysql_databases[] = $data_row["Database"];
		$template->assign( 'mysql_databases', $mysql_databases );

		// select a MySQL db and try to connect to rail
		if( strlen($_POST['db']) > 0 ){
			// throw an exception if databases dosn't exist
			if( !in_array( $_POST['db'], $mysql_databases ) )
				throw new Exception( 'Die Datenbank \'' . $_POST['db'] . '\' existiert nicht.' );

			// connect to rail
			$_SESSION['mysql_db'] = $_POST['db'];
			$rail = new rail_connector( $_SESSION["mysql_db"], $_SESSION["mysql_user"], $_SESSION["mysql_passwd"], $_SESSION["mysql_host"] );
			header( 'Location: index.php?tpl=portal' );
		}
	}
	// if connection to MySQL or rail failed
	catch( Exception $e ){
		// only connection to rail failed
		if( $e instanceof rail_exception )
			$template->assign( 'create_rail', $_POST['db'] );
		// connection to MySQL failed
		else{
			$template->assign( 'error_message', 'Es ist ein Fehler beim Verbinden mit dem MySQL Server aufgetreten: ' . $e->getMessage() );
			session_destroy();
			session_start();
		}
	}
} // Eo form send
// connect to rail if SESSION has a MySQL host and db
else if( isset($_SESSION['mysql_host']) && isset($_SESSION['mysql_db']) ){
	#$rail = new rail_connector( $_SESSION['mysql_db'], $_SESSION['mysql_user'], $_SESSION['mysql_passwd'], $_SESSION['mysql_host'] );

	// select all MySQL databases
	$mysqli = new mysqli( $_SESSION['mysql_host'], $_SESSION['mysql_user'], $_SESSION['mysql_passwd'] );
	$data = $mysqli->query( 'SHOW DATABASES' );
	while( $data_row = $data->fetch_assoc() )
		$mysql_databases[] = $data_row["Database"];
	$template->assign( 'mysql_databases', $mysql_databases );
}


/* -------------------------------------- Display ------------------------------------- */
$template->assign( array(
	'mysql' => array(
		'logedin' => isset($_SESSION['mysql_host']),
		'host' => (isset($_SESSION['mysql_host']) ? $_SESSION['mysql_host'] : 'localhost'),
		'user' => (isset($_SESSION['mysql_user']) ? $_SESSION['mysql_user'] : ''),
		'passwd' => (isset($_SESSION['mysql_passwd']) ? $_SESSION['mysql_passwd'] : ''),
		'db' => (isset($_SESSION['mysql_db']) ? $_SESSION['mysql_db'] : ''),
	)
));

?>